<?php
set_time_limit(0);
include '../../common.inc.php';
$hd = new ImportLoop();
$hd->setSource('mysql:host=192.168.1.19;dbname=caiji', 'root', 'xddlxdcw');
$hd->setAim('mysql:host=192.168.1.19;dbname=ixdcw_v12', 'root', 'xddlxdcw');
$hd->setSourceInfo('sheet1', 'id', '');
$params = array(
	'maxid'     => isset($_GET['maxid']) ? $_GET['maxid'] : 0,
	'id'        => isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : 0,
	'startTime' => isset($_GET['startTime']) ? $_GET['startTime'] : time(),
	'limit'     => 1,
	'interval'  => 1,
	'debug'     => isset($_GET['debug']) ? $_GET['debug'] : 'xddl',
	'markHeight'   => 20,
	'jpegQuality'   => 85,
);
$hd->initParams($params);

$result = $hd->getDbSource()->query('select * from ' . $hd->getSourceTable() . ' where id>' . $hd->getParam('id') . ' and ishandle=0 order by id ASC limit ' . $hd->getParam('limit'));
$rs     = $result->fetchAll();
$sourceFolder = 'O:\pic_11.13';
$aimFolder = 'F:/img1_11.13';

if (!$rs) {
	$hd->end();
} else {
	foreach ($rs as $k => $v) {
		$hd->setParam('id', $v['id']);
		$sourceFolder .= $v['folder'];
		$folderZh = Str::convert($sourceFolder,'utf-8', 'gbk');
		$fileLists = File::listAll($folderZh);
		$time = time()-rand(0, 3600*24*25);
		if (!$fileLists) {
			$sql = Sql::update($hd->getSourceTable(), "ishandle=1, picnum=0, usetime='".$hd->times()."'", "id={$v['id']}");
			$hd->getDbSource()->query($sql);
			continue;
		}
		$inserts = array();
		$picTable = Table::autoPicture($v['arcid']);

		foreach($fileLists as $file) {
			$fileUtf8 = Str::convert($file, 'gbk', 'utf-8');
			$matched = AutoImport::pictureMatchAutohome(str_replace($sourceFolder, '', $fileUtf8));
			var_dump($matched);

			if (!$matched) {
				$matched['typeid'] = 0;
				$matched['desc'] = $v['name'];
			};

			// handle Picture
			$name = Picture::name(14, 1, File::ext($file), $time);
			$aimName = $aimFolder.'/'.$name;

			$inserts[] = array(
				'arbid' => AutoCar::getBrandId($v['arcid']),
				'armid' => AutoCar::getManufactorId($v['arcid']),
				'arcid' => $v['arcid'],
				'amid' => $v['amid'],
				'alid' => $matched['typeid'],
				'title' => $matched['desc'],
				'imgurl' => U::domain(U::DOMAIN_IMG1).$name,
				'addtime' => $DT_TIME,
			);
			$dirname = dirname($aimName);
			if (!file_exists($dirname)) {
				var_dump(mkdir($dirname, 0777, true));
			}
			$info = getimagesize($file);
			try {
				$options = array(
					'jpegQuality' => $hd->getParam('jpegQuality'),
				);
				$thumb = PhpThumbFactory::create($file, $options);
				$thumb->crop(0, $hd->getParam('markHeight'), $info[0], $info[1] - $hd->getParam('markHeight')*2)
					->save($aimName);
				var_dump($aimName.'图片裁切完成');
			} catch (Exception $e) {
				File::append('error_imageImport', Str::convert($e->getMessage(), 'gbk', 'utf-8'));
				continue;
			}

		}
		$inserts = Str::addSlashes($inserts);

		$values = array();
		$sql = "insert into $picTable (".implode(',', array_keys($inserts[0]))." ) values";
		foreach($inserts as $k => $va) {
			$values[] = '(\''.implode("','", $va).'\')';
		}
		$sql .= implode(',', $values);
		if ($hd->getDbAim()->query($sql)) {
			$sql = Sql::update($hd->getSourceTable(), "ishandle=1, picnum=".count($inserts).", usetime='".$hd->times()."'", "id={$v['id']}");
			$hd->getDbSource()->query($sql);
			var_dump($v['arcid'], count($inserts), $hd->times(), $hd->getParam('maxid'));
		} else {
			print_r($sql);
			exit();
		}
	}
}
var_dump($hd->getParams());
echo $hd->start($hd->getParams());